package simpleeditor.controllers;

import java.util.List;

import simpleeditor.Constants;
import simpleeditor.Ui_SimpleEditor;
import simpleeditor.model.Group;
import simpleeditor.model.Property;
import simpleeditor.model.PropertyType;

import com.trolltech.qt.gui.QTableWidgetItem;

public class PropertyController extends TableEditController {

	private static final int PROP_NAME_COLUMN = 0;
	private static final int TYPE_COLUMN = 1;
	private static final int VALUE_COLUMN = 2;
	private GroupController gc;

	public PropertyController(Ui_SimpleEditor ui) {
		super(ui.propertiesTableWidget,ui.addPropertyButton,ui.removePropertyButton);	
	}
	
	public void init(GroupController gc){
		this.gc=gc;
	}
	
	@Override
	public void addRow() {
		Group g;
		String propId;
		
		g=gc.getSelectedGroup();
		propId=g.newProperty();
		
		
		table.setRowCount(table.rowCount()+1);
		table.setItem(table.rowCount()-1, 0, new QTableWidgetItem(propId));
		table.setItem(table.rowCount()-1, 1, new QTableWidgetItem(g.getProperty(propId).getType().toString()));
		table.setItem(table.rowCount()-1, 2, new QTableWidgetItem(g.getProperty(propId).value()));

	}

	public void enableAddProperty(boolean enabled) {
		addButton.setEnabled(enabled);
		if(!enabled){
//			table.clear();
			clear();
		}
	}

	public void showProperties(List<Property> properties) {
		Property p;
		
//		table.clear();
//		System.out.println(properties);
		clear();
		table.setRowCount(properties.size());
		
		for(int i=0;i<properties.size();i++){
			p=properties.get(i);
			table.setItem(i, 0, new QTableWidgetItem(p.getName()));
			table.setItem(i, 1, new QTableWidgetItem(p.getType().toString()));
			table.setItem(i, 2, new QTableWidgetItem(p.value()));
		}
	}

	@Override
	protected void removeFromModel(String item) {
//		System.out.println("Ho rimosso "+observer.getSelectedGroup().removeProperty(item).getName());
		gc.getSelectedGroup().removeProperty(item);
	}

	public void clear() {
		table.setRowCount(0);
		
	}

	@Override
	public void editItem() {

		QTableWidgetItem selected;
		int column,row;
		String propId;
	
		selected=table.selectedItems().get(0);
		column=selected.column();
		
		if(column==TYPE_COLUMN){
			editChoise(TYPE_COLUMN,PropertyType.values());
		}else if(column==PROP_NAME_COLUMN){
			editText(PROP_NAME_COLUMN);
		}else{
			
			row=selected.row();
			propId = table.item(row, PROP_NAME_COLUMN).text();
			
			switch(gc.getSelectedGroup().getProperty(propId).getType()){
			case INTEGER:
				editInteger(VALUE_COLUMN, 0, Constants.MAX_MULTIPLICITY);
				break;
			}
			
		}
		
	}

	@Override
	protected boolean updateModel(int row, int column, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected boolean updateModel(int row, int column, String oldValue, String newValue) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected boolean updateModel(int row, int editedColumn2, int currentIndex,
			String text, String currentText) {
		// TODO Auto-generated method stub
		return false;
	}

}
